import {defineStore} from "pinia";
import {PmTabBarType} from "@/enum";
import {usePmLoginApi} from "@/api/pm/login";
import {usePmMenuStore} from "@/stores";
import {queryToUrl} from "@/utils";

export const PM_ACCESS_TOKEN = 'PM_ACCESS_TOKEN';
export const PM_REFRESH_TOKEN = 'PM_REFRESH_TOKEN';
export const PM_TENANT_ID = '1'
export const PM_ROUTE_PATH = '/pmPages'
export const PM_PROJECT_ROUTE_PATH = '/pmPages/project'

type PmAppState = {
  tabKey: PmTabBarType,
  loginRedirectUrl: string
  userInfo: PmUserInfo
}

const PmAppState: PmAppState = {
  tabKey: PmTabBarType.TODO,
  loginRedirectUrl: `${PM_ROUTE_PATH}/index/index`,
  userInfo: {
    id: 0, // 用户编号
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    switchUserList: [],
    menus: [],
  }
}

export const usePmAppStore = defineStore('PmApp', {
  state: (): PmAppState => ({
    ...PmAppState
  }),
  actions: {
    setTabKey(val: PmTabBarType): void {
      this.tabKey = val;
    },
    setToken(token: string) {
      this.clearUserInfo();
      uni.setStorageSync(PM_ACCESS_TOKEN, token);
    },
    setRefreshToken(token: string) {
      uni.setStorageSync(PM_REFRESH_TOKEN, token);
    },
    getToken() {
      return uni.getStorageSync(PM_ACCESS_TOKEN);
    },
    removeToken() {
      uni.removeStorageSync(PM_ACCESS_TOKEN);
    },
    getRefreshToken() {
      return uni.getStorageSync(PM_REFRESH_TOKEN);
    },
    removeRefreshToken() {
      uni.removeStorageSync(PM_REFRESH_TOKEN);
    },
    setLoginRedirectUrl(url: string | null) {
      this.loginRedirectUrl = url;
    },
    getPasskeyAuthCode(redirectUrl: string) {
      if (redirectUrl.includes(PM_ROUTE_PATH)) {
        this.loginRedirectUrl = redirectUrl;
      } else {
        this.loginRedirectUrl = `${PM_ROUTE_PATH}/index/index`;
      }
      uni.redirectTo({ url: '/pmPages/login/index' });
    },
    login(data?: PmLoginParams) {
      return new Promise((resolve, reject) => {
        // this.setToken('ed0ff5b0d6244e36b495bda6b4189922')
        console.log(this.getToken())
        resolve(true)
        // usePmLoginApi().socialLogin(data).then(res => {
        //   console.log(res)
        //   resolve()
        // })
      })
    },
    getInfo() {
      return new Promise((resolve, reject) => {
        usePmLoginApi().getInfo().then((res: any) => {
          if (res.code === 0 && res.data) {
            this.userInfo.roles = res.data.roles
            this.userInfo.permissions = res.data.permissions
            this.userInfo.id = res.data.user.id
            this.userInfo.name = res.data.user.nickname
            this.userInfo.menus = res.data.menus
            usePmMenuStore().generateMenus(JSON.parse(JSON.stringify(this.userInfo.menus)))
          }
        }).finally(() => {
          resolve(true)
        })
      })
    },
    clearUserInfo() {
      this.userInfo = {
        id: 0, // 用户编号
        name: '',
        avatar: '',
        roles: [],
        permissions: [],
        switchUserList: [],
        menus: [],
      }
    },
    clearUserAndToken() {
      this.removeToken();
      this.removeRefreshToken();
      this.clearUserInfo();
    },
  },
  persist: true,
})
